package com.woniuxy.microcontentserver.advertisemsg.mapper;

import com.woniuxy.microcontentserver.bean.AdvertiseBean;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdvertiseMapper {
    /**
     * 查询所有广告
     * @param advertiseBean
     * @return
     */

    List<AdvertiseBean> findAll(@Param("a") AdvertiseBean advertiseBean);


    /**
     * 根据id查询栏目
     * @param id
     * @return
     */
    @Select("select id,name,image,jump_address as jumpAddress,position,status,advertise_Time as advertiseTime from advertise_info where id=#{id}")
    AdvertiseBean getOneById(Long id);
    /*  @Select("select id,role_name as roleName,role_des as roleDes,create_time as createTime,state,useing,permit from role_info where id = #{id}")
    RoleInfo findById(Integer id);*/

    /*@ResultType(Category.class)
    @Select(value = {" <script>" +
            " SELECT * FROM category " +
            " <where> 1=1 " +
            " <if test=\"category.categoryName != null\"> AND category_name like concat('%', #{category.categoryName}, '%') </if> " +
            " <if test=\"category.createTime != null\" >  AND create_time=#{category.createTime}</if> " +
            " </where>" +
            " </script>"})
    @Results({
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "categoryName", column = "category_name"),
            @Result(property = "categoryOrder", column = "category_order"),
            @Result(property = "subcategory", column = "subcategory"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "categoryDescription", column = "category_description"),
            @Result(property = "categoryStatus", column = "category_status")
    })
    List<Category> getCategoriesByMultiConditions(@Param("category") Category category);*/
    @ResultType(AdvertiseBean.class)
    @Select(value = {" <script>" +
            " SELECT * FROM advertise_info " +
            " <where> 1=1 " +
            " <if test=\"a.name != null\"> AND name like concat('%', #{a.name}, '%') </if> " +
            " <if test=\"a.position != null\" >  AND position=#{a.position}</if> " +
            " <if test=\"a.status != null\" >  AND status=#{a.status}</if> " +
            " </where>" +
            " </script>"})
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "image", column = "image"),
            @Result(property = "jumpAddress", column = "jump_address"),
            @Result(property = "position", column = "position"),
            @Result(property = "status", column = "status"),
            @Result(property = "advertiseTime", column = "advertise_time")
    })
    List<AdvertiseBean> findByCondition(@Param("a") AdvertiseBean advertiseBean);

    /**
     * 新增广告
     * @param advertiseBean
     * @return
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "a.id"
    )
    @Insert("insert into advertise_info"+
            "(name,image,jump_address,position,status,advertise_time)"+
            "values(#{a.name},#{a.image},#{a.jumpAddress},#{a.position},#{a.status},#{a.advertiseTime})"
    )
    int addAdvertiseBean(@Param("a") AdvertiseBean advertiseBean);

    /**
     * 修改广告
     * @param advertiseBean
     * @return
     */
    @Update("<script>"+
            "update advertise_info"+
            "<set>"+

            "<if test='a.name != null'>" +
            " name = #{a.name}," +
            "</if>" +

            "<if test='a.image != null'>" +
            " image = #{a.image}," +
            "</if>" +

            "<if test='a.jumpAddress != null'>" +
            " jump_address = #{a.jumpAddress}," +
            "</if>" +
            "<if test='a.position != null'>" +
            " position = #{a.position}," +
            "</if>" +

            "<if test='a.status != null'>" +
            " status = #{a.status}," +
            "</if>" +

            "<if test='a.advertiseTime != null'>" +
            " advertise_time = #{a.advertiseTime}," +
            "</if>" +

            "</set>"+
            "where id=#{a.id}"+
            "</script>"
    )
    int updateAdvertiseBean(AdvertiseBean advertiseBean);

    /**
     * 删除栏目
     * @param id
     * @return
     */
    @Delete("delete from advertise_info where id=#{id}")
    int deleteAdvertiseBean(@Param("id") Long id);

}
